<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
/*	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在！请重新约课','error');
	}
	$order=pdo_get('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	*/
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	$lesson=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['lessonid']));
	$store=pdo_get('jy_edu_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['storeid']));
	$user=pdo_get('jy_edu_user',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
//	$this->session('order_id',$order_id);
	$post=array('isfee'=>$_RQ['isfee'],'lessonid'=>$_RQ['lessonid'],'storeid'=>$_RQ['storeid'],'workerid'=>$_RQ['workerid'],'dayid'=>$_RQ['dayid']);
	if($_RQ['isfee']){
		$lesson['price']=0.00;
	}
	$return['bodys']=pdo_getall('jy_edu_body',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['lesson']=$lesson;
	$return['day']=$data[$_RQ['dayid']];
	$return['store']=$store;
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		if(!$postdata['bodyid'] && (!$postdata['name'] || !$postdata['agetime'])){
			exi('请选择报名小孩或填写完整报名信息','error');
		}
		$data=array();
		$data['0']=date("Y-m-d",time("day")); 
		$data['1']=date("Y-m-d",strtotime("+1 day")); 
		$data['2']=date("Y-m-d",strtotime("+2 day")); 
		$data['3']=date("Y-m-d",strtotime("+3 day")); 
		$data['4']=date("Y-m-d",strtotime("+4 day")); 
		$data['5']=date("Y-m-d",strtotime("+5 day")); 
		$data['6']=date("Y-m-d",strtotime("+6 day"));
		if($postdata['isfee']){
			$price=0;
		}else{
			$price=pdo_getcolumn('jy_edu_lesson',array('id'=>$postdata['lessonid']),'price');
		}
		$new_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'storeid'=>	$postdata['storeid'],
				'bodyid'=>	$postdata['bodyid'],
				'lesson_id'=>$postdata['lessonid'],
				'worker_id'=>$postdata['workerid'],
				'money'=>$price,
				'day'=>$data[$postdata['dayid']],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_edu_order','ordersn'),
				'createtime'=>TIMESTAMP,
		);
		$id=model('jy_edu/order')->create($new_order,'',$url);
		$this->session('order_id',$id);
		$params=array(
			'id'=>$id,
			'title'=>'约课',
			'fee'=>$price,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_edu/confirm/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_edu/order/detail/id='.$order_id,
			'error_url'=>'jy_edu/order/detail/id='.$order_id,
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_edu_order',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
		);
		exi($res);
}
if($_RQ['op']=='uporder'){
	$post=get_postdata();
	if(!$post){
		exi('信息错误，请重新提交','error');
	}
	$order_id=$this->session('order_id');
	$order=pdo_get('jy_edu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_edu_order',array('status'=>'1','pay'=>0),array('id'=>$order['id']));
	exi('约课成功！','success','jy_edu/order/list/status=1&id='.$order['id']);
}
if($_RQ['op']=='confirmpay'){
	$order_id=intval($_RQ['id']);
	$this->session('order_id',$id);
	if(!$order_id){
		exi('订单不存在！请重新约课','error');
	}
	$order=pdo_get('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	$lesson=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['lesson_id']));
	$return['lesson']=$lesson;
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_edu/order/');
	}
	$order=pdo_get('jy_edu_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_edu/order/');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['pay'].'|'.$payresult['fee'],'error','jy_edu/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
	);
	$url=custom_url('jy_edu/order/detail/id='.$order['id'],true);
	model('jy_edu/order')->order_pay($order['id'],$params,$url);
	exi('支付成功!');
}